package com.truyentranh.network;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import com.truyentranh.utils.Logger;

public class UrlHelper {

	public static final String dev = "http://192.168.1.4";
	public static final String live = "http://haibl.net";
	public static final String base = live;

	public static final String BASE_URL_HAIVL = base + "/public/index.php?";
	public static final String BASE_URL_CAB = base + "/public/cab?";
	public static final String BASE_URL_EPIC = base + "/public/epic?";
	public static final String BASE_URL_TAMDIEM = base + "/public/tamdiem?";
	public static final String BASE_URL_OHVUI = base + "/public/ohvui?";
	public static final String BASE_URL_TAMDIEM_DETAIL = base + "/public/tamdiem/detail?";
	public static final String BASE_URL_UPDATES = base + "/public/update?";
	public static final String BASE_URL_ADS = base + "/public/ads?";
	public static final String BASE_URL_GIRL = base + "/public/girl?";
	public static final String BASE_URL_DETAIL = base + "/public/girl/detail?";

	public static final String BASE_URL_NEWS_ALL = base + "/news/index.php/news/getAllNews?";
	public static final String BASE_URL_RSS_TITLE_ALL = base + "/news/index.php/news/getAllRssTitleByNewsId?";
	public static final String BASE_URL_RSS_ALL = base + "/news/index.php/news/getAllRssContentByNewsId?";
	public static final String BASE_URL_TRUYEN_TRANH = base + "/public/truyentranh/";

	private static final String KEY = "963852";
	private static final String ACCESS_KEY = "s82uf74hf0skf8rt";

	private static List<String> list = new LinkedList<String>();

	public static String getUrlOfRequestUpdates(String baseUrl) {
		if (baseUrl == null || baseUrl.equals("") ) {
			return "";
		} else {

			StringBuffer bufferParams = new StringBuffer();
			StringBuffer bufferGet = new StringBuffer(baseUrl);

			// complete params
			long time = System.currentTimeMillis();
			bufferParams.append("key").append("=").append(KEY);
			bufferParams.append("ts").append("=").append(time);
			bufferGet.append("key").append("=").append(KEY).append("&");
			bufferGet.append("ts").append("=").append(time).append("&");
			// Get sig
			Logger.logMe("bufferParams = "+bufferParams.toString());
			String sig = buildSig(bufferParams);
			if (sig != null) {
				bufferGet.append("sig").append("=").append(sig);
			}
			return bufferGet.toString();
		}
	}

	public static String getUrlOfMethod(String baseUrl,
			String[] paramsName, String[] paramsValue) {
		if (baseUrl == null || baseUrl.equals("") ) {
			return "";
		} else if (paramsName == null || paramsValue == null) {
			return null;
		} else if (paramsName.length != paramsValue.length) {
			return null;
		} else {
			int length = paramsName.length;

			StringBuffer bufferParams = new StringBuffer();
			StringBuffer bufferGet = new StringBuffer(baseUrl);
			for (int i = 0; i < length; i++) {
				String key = paramsName[i];
				String value = paramsValue[i].trim().replaceAll(" ", "%20");
				bufferParams.append(key).append("=").append(paramsValue[i]);
				bufferGet.append(key).append("=").append(value).append("&");
			}
			// complete params
			long time = System.currentTimeMillis();
			bufferParams.append("key").append("=").append(KEY);
			bufferParams.append("ts").append("=").append(time);
			bufferGet.append("key").append("=").append(KEY).append("&");
			bufferGet.append("ts").append("=").append(time).append("&");
			// Get sig
			Logger.logMe("bufferParams = "+bufferParams.toString());
			String sig = buildSig(bufferParams);
			if (sig != null) {
				bufferGet.append("sig").append("=").append(sig);
			}
			return bufferGet.toString();
		}
	}

	public static List<NameValuePair> getValueOfMethodPost(String root,
			String methodName, String[] params, String[] values) {
		List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

		nameValuePairs.clear();

		nameValuePairs.add(new BasicNameValuePair("method", methodName));
		nameValuePairs.add(new BasicNameValuePair("key", KEY));

		int len = params.length;
		for (int i = 0; i < len; ++i) {
			nameValuePairs.add(new BasicNameValuePair(params[i], values[i]));
		}

		StringBuilder builder = new StringBuilder();
		String temp;

		list.clear();

		builder.append(root);
		temp = "method=" + methodName;
		list.add(temp);
		builder.append(temp);

		temp = "key=" + KEY;
		list.add(temp);
		builder.append("&" + temp);

		for (int i = 0; i < len; ++i) {
			list.add(params[i] + "=" + values[i]);
			builder.append("&" + params[i] + "=" + values[i]);
		}
		String url = builder.toString();

		Collections.sort(list);
		builder = new StringBuilder();
		for (String str : list) {
			builder.append(str);
		}

		String sig = com.truyentranh.utils.StringUtils.MD5(builder.toString());

		nameValuePairs.add(new BasicNameValuePair("sig", sig));

		return nameValuePairs;
	}

	private static String buildSig(StringBuffer buffer) {
		if (buffer == null || buffer.length() <= 0) {
			return null;
		} else {
			buffer.append(ACCESS_KEY);
			String params = buffer.toString();
			return com.truyentranh.utils.StringUtils.MD5(params);
		}
	}

}
